Repository navigation
Conversation
macoladik
left a comment
There was a problem hiding this comment.
Nine findings: how the new resolution behaves when $0 is not the script, one silent failure path, and six test gaps that let the readlink/cwd regressions back in.
| self=$0 | ||
| # bash passes a slashless $0 when the script is found via PATH; resolve it | ||
| # unless a same-named file in cwd is what bash actually opened. | ||
| if [ ! -f "${self}" ] && resolved=$(command -v -- "${self}"); then |
There was a problem hiding this comment.
When $0 is not the script - bash -s < entry.sh job-42 /var/base, or a wrapper doing bash -c 'source ./entry.sh' job-42 ... - $0 is bash, [ -f bash ] is false and command -v bash succeeds, so self becomes /bin (or /usr/bin on Linux) and judges is invoked as --lib /bin/lib /bin/judges. The command -v judges check at line 25 already passed, so the job dies inside judges with a message that never mentions $0. Minimal fix, after the loop: [ -d "${self}/judges" ] || { echo >&2 "Cannot locate the swarm directory for '$0' (resolved to '${self}')"; exit 1; }.
| *) self=$(dirname "${self}")/${link} ;; | ||
| esac | ||
| done | ||
| self=$(cd "$(dirname "${self}")" && pwd -P) |
There was a problem hiding this comment.
set -e turns a failed cd into a bare exit 1 with no output: run with $0=/mnt/job/entry.sh once the job volume is unmounted, or when the directory loses +x, and the swarm run fails with an empty log - judges never starts and nothing says why. Fix: self=$(cd "$(dirname "${self}")" && pwd -P) || { echo >&2 "Cannot enter '$(dirname "${self}")'"; exit 1; }.
|
|
||
| def test_keeps_own_directory_when_slashless_script_is_not_in_path | ||
| Dir.mktmpdir do |dir| | ||
| FileUtils.cp(File.expand_path('../entry.sh', __dir__), File.join(dir, 'entry.sh')) |
There was a problem hiding this comment.
This test cannot fail for the behaviour it names: bin, the only PATH entry prepended in run_entry (line 97), contains just judges, so command -v entry.sh never succeeds here and the [ ! -f "${self}" ] guard at entry.sh:33 is irrelevant - deleting that guard keeps this test green. To lock the rule, put a second entry.sh with its own lib/ inside bin and assert the cwd copy's directory wins (bash opens the literal name first, so the PATH copy is never executed).
| home = File.join(dir, 'home') | ||
| cwd = File.join(dir, 'cwd') | ||
| FileUtils.mkdir_p([home, cwd]) | ||
| passed = run_entry(bin, home, File.join(dir, 'args.txt'), script: %w[bash entry.sh], chdir: cwd) |
There was a problem hiding this comment.
The branch the guard exists for is untested: a copy of entry.sh in the cwd and on PATH. An implementation that always prefers the PATH lookup (resolved=$(command -v -- "${self}") || true; self=${resolved:-$0}) passes this test (entry.sh only on PATH) and the test at line 58 (entry.sh only in cwd), yet resolves the wrong directory whenever both exist. Add that case here, with distinct lib/ directories for each copy.
| stub = File.join(bin, 'readlink') | ||
| File.write(stub, <<~SH) | ||
| #!/bin/sh | ||
| [ "$1" = '-f' ] && { echo 'readlink: illegal option -- f' >&2; exit 1; } |
There was a problem hiding this comment.
The stub rejects only -f, so every other GNU-only flag slips through: replacing the readlink call at entry.sh:37 with readlink -e or readlink --canonicalize keeps this test green and re-breaks macOS (issue #298). Reject any option: [ "${1#-}" != "$1" ] && { echo 'readlink: illegal option' >&2; exit 1; }.
| File.write(stub, <<~SH) | ||
| #!/bin/sh | ||
| [ "$1" = '-f' ] && { echo 'readlink: illegal option -- f' >&2; exit 1; } | ||
| exec /usr/bin/readlink "$@" |
There was a problem hiding this comment.
Hardcoding /usr/bin/readlink breaks on hosts that ship it as /bin/readlink only (no usr-merge): the stub then exits 127, link in entry.sh is empty, resolution silently stops at the symlink's own directory and the test fails with a wrong path instead of a portability signal. Resolve the real binary once in Ruby (File.realpath('/bin/readlink'), falling back to /usr/bin/readlink) and exec that path in the stub.
|
|
||
| def run_entry(bin, home, args, script: './entry.sh') | ||
| def run_entry(bin, home, args, script: './entry.sh', chdir: Dir.pwd) | ||
| stdout, stderr, status = Open3.capture3( |
There was a problem hiding this comment.
The judges stub truncates its output file (File.write at line 16), so a second invocation is invisible: a retry loop, or a pre-flight judges --version added to entry.sh, leaves every test green while only the last call is asserted. Make the stub append (File.open(ENV.fetch('JUDGES_ARGS_FILE'), 'a')) and assert the expected number of invocation blocks.
| @@ -31,6 +30,43 @@ def assert_real_path_used(passed) | |||
| assert_equal(File.expand_path('../judges', __dir__), passed[passed.index('--lib') + 2]) | |||
There was a problem hiding this comment.
passed.index('--lib') is nil when the flag is dropped or renamed, so the regression raises NoMethodError: undefined method '+' for nil instead of naming the problem - both assertions here do it. Guard first: i = passed.index('--lib'); refute_nil(i, "--lib not passed: #{passed.inspect}") and index from i.
| judges_stub(dir), "#{dir}/home", "#{dir}/args.txt", script: %w[bash entry.sh], chdir: dir | ||
| ) | ||
|
|
||
| assert_equal(File.join(File.realpath(dir), 'lib'), passed[passed.index('--lib') + 1]) |
There was a problem hiding this comment.
The test creates judges/ (line 61) but asserts only the --lib value, so the second path the script passes - ${self}/judges from entry.sh:46 - is unverified: a regression passing ./judges or the cwd instead of ${self}/judges stays invisible. Add assert_equal(File.join(File.realpath(dir), 'judges'), passed[passed.index('--lib') + 2]).
yegor256
left a comment
There was a problem hiding this comment.
The directory lookup still depends on the caller's CDPATH, which can send judges an invalid or unrelated swarm path.
| *) self=$(dirname "${self}")/${link} ;; | ||
| esac | ||
| done | ||
| self=$(cd "$(dirname "${self}")" && pwd -P) |
There was a problem hiding this comment.
[P2] Clear CDPATH before resolving the script directory. With an exported CDPATH=. and a relative invocation such as bash swarm/entry.sh job-42 /existing/home, cd swarm prints its destination before pwd -P, so self contains two lines and both paths passed to judges are invalid; a CDPATH pointing elsewhere can also select a different swarm directory. Unset CDPATH inside this command substitution before calling cd, and add a regression test invoking the script through a relative subdirectory with CDPATH set.
entry.shresolved its own directory withreadlink -f, which BSDreadlinkon macOS rejects, so the documented entry point died with "illegal option -- f" before a single judge ran, while the README promised only Ruby and Bundler. It now walks symlink chains with plainreadlinkandcd … && pwd -P, and a slashless$0found throughPATHresolves to the script instead of the working directory.test_resolves_relative_symlink_chain_without_gnu_readlinkruns the script end to end with areadlinkshim that rejects-f, so Linux CI covers the BSD host case;test_resolves_slashless_script_name_found_through_pathpins the PATH invocation.Closes #298